<?php

abstract class bannersDAOImpl {
    public static function Select($search=NULL) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "select 
                id, viewed, type, filepicture, id_enterprise,
                href,target,title, expiration
                from banners ";
            if(isset($search)){
                $query = $query . " where id=?";
            }
            DB::$_stmt = DB::$mysqli->prepare($query);
            if(isset($search)){
                DB::$_stmt->bind_param('i',$search);
            }
            if (!DB::$_stmt->execute()) {
                DB::$mysqli->error;
            }
            DB::$_stmt->bind_result(
                    $data_id,
                    $data_viewed,
                    $data_type,
                    $data_filepicture,
                    $data_id_enterprise,
                    $data_href,
                    $data_target,
                    $data_title,
                    $data_expiration
                    );
            while (DB::$_stmt->fetch()) {
                $data[] = array(
                    'id'=>$data_id,
                    'viewed'=>$data_viewed,
                    'type'=>$data_type,
                    'filepicture'=>$data_filepicture,
                    'id_enterprise'=>$data_id_enterprise,
                    'href'=>$data_href,
                    'target'=>$data_target,
                    'title'=>$data_title,
                    'expiration'=>$data_expiration
                );
            }
            return $data;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Update($type,$filepicture,$id_enterprise,$href,$target,$title,$expiration,$id) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "update banners set
                type=?,filepicture=?,id_enterprise=?,href=?,target=?,title=?,expiration=? where id=?";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('isisssi',$type,$filepicture,$id_enterprise,$href,$target,$title,$expiration,$id);
            if (!DB::$_stmt->execute()) {
                echo DB::$mysqli->error;
            }
            return DB::$mysqli;
        } else {
            error_log("erro ao conectar!");
        }
    }
    
    public static function Insert($type,$filepicture,$id_enterprise,$href,$target,$title,$expiration) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "insert into banners 
                (type,filepicture,id_enterprise,href,target,title,expiration) 
                values (?,?,?,?,?,?,?)";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('isissss',$type,$filepicture,$id_enterprise,$href,$target,$title,$expiration);
            if (!DB::$_stmt->execute()) {
                echo DB::$mysqli->error;
            }
            return DB::$mysqli->insert_id;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Delete($id) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "delete from banners where id = ?";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('i', $id);
            if (!DB::$_stmt->execute()) {
                echo DB::$mysqli->error;
            }
            return DB::$_stmt;
        } else {
            error_log("erro ao conectar!");
        }
    }
}

?>
